#!/bin/bash
# Computes number of periods with given concurrency 
# (input = simula.pl stdout)

#grep running | cut -f1 -d: | sort -n | uniq -c | sort -n | tr -s " " | cut -f2 -d" " | uniq -c > /tmp/plot

echo -e "\n    STARTING figures re-construction"
echo -e "    Takes minutes to complete \n"

Tmax=1000000
[ $1 ] && Tmax=$1

# The following is the configuration that corresponds to the
# use case presented in the paper. The time unit in the simulation 
# corresponds to 0.1 seconds: the time scales in figures are
# modified accordingly.
Dop=40
Dmin=6000
ggenerate=1800000
gloss=100000
# Another interesting use case
# Dop=10; Dmin=1000; ggenerate=200000; gloss=1000000"

NLIST="210 300 360"

#***************************

echo -e "\n    Regenerating simulation results \n"
echo -e "Duration for all simulations is $Tmax time units"
echo -e "(use the command-line parameter to indicate a different duration)\n"

for n in $NLIST; do 
	echo "Starting token simulation with $n components"
	./simula.pl -N $n -t $Tmax -o $Dop -m $Dmin -g $ggenerate -l $gloss > simul-$Tmax\x$n.dat
	echo -e "Done\n"
done
echo "Starting benchmark simulation"
./simula-random.pl $Tmax > /dev/null
echo -e "Done\n"

echo -e "   Regenerating graphics \n"

#***************************

echo "Stabilization"

for n in $NLIST; do
	perl -e 'while ( <> ) {( /(\d+): (\d+) active/ ) || next;print "$1 $2\n";}' < simul-$Tmax\x$n.dat > paper-simul-WT-210-stab.dat 

	gnuplot <<EOF
  		set terminal postscript eps
		set output 'stabilizzazione-$n.ps'
		set size 0.7,0.4
		set ytics 0,1
		set ylabel 'n. of tokens'
		set xlabel 'time'
		set title 'Number of tokens per time unit (stabilization)'
		plot 'paper-simul-WT-210-stab.dat' using 1:2 with steps title "N=$n"
EOF
done

#***************************

echo "Concurrency"

for n in $NLIST; do
	perl -e 'while ( <> ) {( /(\d+):.*running/ ) || next;print "$1\n";}' <simul-$Tmax\x$n.dat | 
	uniq -c | 
	sort -n | 
	tr -s " " | 
	cut -f2 -d" " | 
	uniq -c | 
	./complete.pl $Tmax > paper-simul-WT-$n.dat
done

gnuplot <<EOF
  set terminal postscript eps
  set output 'concorrenza.ps'
  set size 0.7,0.5
  set xtics 0,1
  set ylabel 'percent of time units'
  set xlabel 'n. of processed events'
  set title 'Number of processed events per time unit (concurrency)'
  plot [-0.5:] 'paper-simul-WT-210.dat' using 2:(column(1)*100/$Tmax) with histeps lw 3 title 'N=210',\
               'paper-simul-WT-300.dat' using 2:(column(1)*100/$Tmax) with histeps lw 3 title 'N=300',\
               'paper-simul-WT-360.dat' using 2:(column(1)*100/$Tmax) with histeps lw 3 title 'N=360'
EOF

#****************************

echo "Return time distribution"

for n in $NLIST; do
	perl -e 'while ( <> ) {( /.+:.+: (\d+)/ ) || next;print "$1\n";}' < simul-$Tmax\x$n.dat | 
	./histo.pl $Dmin > paper-histo-$n-interarr.dat
done	

gnuplot <<EOF
  set terminal postscript eps
  set output 'histo-interarr.ps'
  set size 0.7,0.5
  set xtics 0,$Dmin/10
  set ylabel 'percent of events'
  set xlabel 'interarrival time'
  set title 'Distribution of interarrival times'
  plot 'paper-histo-210-interarr.dat' using (column(1)/10-300):2 with histeps lw 3 title 'N=210',\
       'paper-histo-300-interarr.dat' using (column(1)/10-300):2 with histeps lw 3 title 'N=300',\
       'paper-histo-360-interarr.dat' using (column(1)/10-300):2 with histeps lw 3 title 'N=360'
EOF

#****************************

echo "Concurrency: comparison with benchmark"

gnuplot <<EOF
  set terminal postscript eps
  set output 'confronto-concorrenza.ps'
  set size 0.7,0.5
  set xtics 0,1
  set ylabel 'percent of time units'
  set xlabel 'n. of processed events'
  set title 'Number of processed events per time unit (concurrency)'
  plot [-0.5:4] 'paper-simul-WT-300.dat' using 2:(column(1)*100/$Tmax) with histeps lw 3 title 'wandering token',\
                'random-histo-300-conc.dat' using 1:(column(2)*100/$Tmax) with histeps lw 3 title 'benchmark'
EOF

#display confronto-concorrenza.ps &

echo "Cleaning..."
#rm *.dat
